package com.stars.home.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.qmuiteam.qmui.kotlin.onClick
import com.stars.home.activity.MainActivity
import com.stars.home.base.MBFragment
import com.stars.home.config.ConfigRx
import com.stars.home.databinding.FragmentHomeMeBinding
import com.stars.home.inte.ChoiceData
import com.stars.home.inte.ChoiceLocation
import com.stars.home.utils.CleanDataUtils
import com.stars.home.utils.CurrentOneLocationUtils
import com.stars.home.utils.PopuSwitchUtils
import com.stars.home.vmodel.HomeMeVModel
import com.tencent.mmkv.MMKV
import java.util.*

/**
 *on 2022/9/22
 */
class HomeMeFragemnt: MBFragment<HomeMeVModel, FragmentHomeMeBinding>() {


    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        vm.getUserInfo()
        vm.getNotMsg()

        try {
            vm.cacheSize.value = CleanDataUtils.getTotalCacheSize(Objects.requireNonNull(context()))
        } catch (e: Exception) {
            e.printStackTrace()
        }

        vm.user.observe(this) {

            if (it.type == 0 && it.status == 0) {
                MMKV.defaultMMKV().putString("isRecord","0")// 未实名认证


            }
            if (it.type == 0 && it.status == 1) {
                MMKV.defaultMMKV().putString("isRecord","1")// 实名认证审核中

            }
            if (it.type == 0 && it.status == -1) {
                MMKV.defaultMMKV().putString("isRecord","-1")// 实名认证不通过

            }

            if(it.status==2&& it.isOnline==0){
                MMKV.defaultMMKV().putString("isRecord","3")// 未开启接单按钮

            }

            if(it.status==2&& it.isOnline==1){
                MMKV.defaultMMKV().putString("isRecord","4")// 已开启接单按钮

            }


        }



    }

    override fun listeners() {
        super.listeners()
        binding.llClearSize.onClick {
            CleanDataUtils.clearAllCache(Objects.requireNonNull(context()))
            vm.cacheSize.value = CleanDataUtils.getTotalCacheSize(Objects.requireNonNull(context()))
            toast("缓存清理成功！")


        }
        binding.llLoad.onClick{


            lifecycleScope.launchWhenCreated {
                CurrentOneLocationUtils.getStartLocal(context() as MainActivity, object :
                    ChoiceLocation {
                    override fun onRestuse(lat: String, lng: String, city: String) {
                        vm.toUpdateLocation(lat,lng)
                    }
                })
            }

        }

        binding.imgBut.onClick {
            if(vm.user.value?.status==1){
                toast("您的实名信息正在认证中，无法修改接单状态！")
                return@onClick
            }
            vm.setStatus()


        }
    }

    override fun vmClass(): Class<HomeMeVModel> {
        return HomeMeVModel::class.java
    }
    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentHomeMeBinding {
        return FragmentHomeMeBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return null
    }

    override fun onRefresh() {
        super.onRefresh()
        vm.getUserInfo()
        vm.getNotMsg()
    }

    override val isRxbus: Boolean
        get() = true

    @Subscribe(tags = [Tag(ConfigRx.REFRESH_USER)])
    fun setRefreshUser(type: String) {
        vm.getUserInfo()
    }

    @Subscribe(tags = [Tag(ConfigRx.MSG_RED)])
    fun setRefreshMsg(position: String) {
        //刷新未读消息数量
        vm.getNotMsg()

    }

    @Subscribe(tags = [Tag(ConfigRx.CERTIFICATION_TO)])
    fun setCertificationTo(type: String) {
        if(type=="2") {
            vm.getUserInfo()
        }

    }

}
